package cn.jbolt.ec.order_return;

import cn.jbolt.base.JBoltBaseService;
import cn.jbolt.common.model.*;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Record;

import java.util.List;

/**
 * @author xieguo
 */
public class OrderReturnService extends JBoltBaseService<BecRetailerOrderReturn> {
    private BecRetailerOrderReturn dao=new BecRetailerOrderReturn().dao();
    @Override
    protected BecRetailerOrderReturn dao() {
        return dao;
    }

    public boolean order_return(int order_id,int[][] commodities,String reason,String delivery_no){
        BecRetailerOrder temp4=new BecRetailerOrder();
        int member_id =temp4.findById(order_id).getClientId();
        for (int i = 0; i <commodities[0].length ; i++) {
            BecRetailerOrderCommodity temp=new BecRetailerOrderCommodity();
            BecRetailerOrderCommodity orderCommodity=temp.findById(commodities[0][i]);
            BecRetailerOrderReturn temp1=new BecRetailerOrderReturn();
            BecRetailerOrderReturn orderReturn=temp1.findFirst("SELECT SUM(amount) as \"sum\" FROM " +
                    "bec_retailer_order_return WHERE order_id=? AND commodity_id=?",order_id,commodities[0][i]);

            if (commodities[1][i]<1){
                return false;
            }

            if (null==orderReturn.getInt("sum")){

            }else {
               if (orderReturn.getInt("sum")+commodities[1][i]>orderCommodity.getAmount()){
                   return false;
               }
            }

            if (commodities[1][i]>orderCommodity.getAmount()){
                return false;
            }
        }

//        for (int i = 0; i <commodities[0].length ; i++) {
//            BecRetailerOrderCommodity temp=new BecRetailerOrderCommodity();
//            BecRetailerOrderCommodity orderCommodity=temp.findById(commodities[0][i]);
//            BecRetailerOrderReturn temp1=new BecRetailerOrderReturn();
//            temp1.setOrderId(order_id).setTimeSubmit(System.currentTimeMillis()).setMemberId(member_id).setDeliveryNo(delivery_no)
//                    .setReason(reason).setRetailerId(orderCommodity.getRetailerId()).setCommodityId(orderCommodity.getId())
//                    .setCommodityName(orderCommodity.getCommodityName()+"  "+orderCommodity.getOptionTitle())
//                    .setRetailerName(new BecRetailer().findById(orderCommodity.getRetailerId()).getName())
//                    .setStatus(1).setPrice(orderCommodity.getPrice()).setAmount(commodities[1][i]).save();
//        }
        return true;

    }

    public List<BecRetailerOrderReturn> check_by_status(int member_id,int status){
        Kv cond =Kv.by("member_id",member_id).set("status",status);
        return getCommonList("id,order_id,time_submit,time_audit,time_refund,commodity_name,commodity_id,amount,price",cond);
    }

    public List<Record> get_info_by_id(int id){
        Kv param =Kv.by("id",id);
        return dbTemplate("ec.mall.shop.order.get_info_by_id",param).find();
    }
}
